﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Wechat.Util.HanderUtil
{
    public class RequestService
    {
        /// <summary>
        /// 判断客户端请求是否是GET请求
        /// </summary>
        /// <returns></returns>
        public static bool IsGet()
        {
            return HttpContext.Current.Request.HttpMethod.Equals("GET");
        }
        /// <summary>
        /// 判断客户端请求是否是Post请求
        /// </summary>
        /// <returns></returns>
        public static bool IsPost()
        {
            return HttpContext.Current.Request.HttpMethod.Equals("POST");
        }
        /// <summary>
        /// 得到Get请求参数，如果为空返回一个整数
        /// </summary>
        /// <param name="strname">参数名称</param>
        /// <param name="defaultval"></param>
        /// <returns>如果为空返回一个默认值</returns>
        public static int RequestQueryInt(string strname, int defaultval)
        {
            if (HttpContext.Current.Request.QueryString[strname] == null || string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[strname].ToString()))
            {
                return defaultval;
            }
            else
            {
                return Convert.ToInt32(HttpContext.Current.Request.QueryString[strname].ToString());
            }
        }
        /// <summary>
        /// 得到Get请求参数
        /// </summary>
        /// <param name="strname">参数名称</param>
        /// <returns>返回整型</returns>
        public static int RequestQueryInt(string strname)
        {
            return Convert.ToInt32(HttpContext.Current.Request.QueryString[strname.ToLower()].ToString());
        }

        /// <summary>
        /// 得到Post请求参数，如果为空返回一个整数
        /// </summary>
        /// <param name="strname">参数名称</param>
        /// <param name="defaultval"></param>
        /// <returns>如果为空返回一个默认值</returns>
        public static int RequestFormInt(string strname, int defaultval)
        {
            if (HttpContext.Current.Request.Form[strname] == null || string.IsNullOrEmpty(HttpContext.Current.Request.Form[strname].ToString()))
            {
                return defaultval;
            }
            else
            {
                return Convert.ToInt32(HttpContext.Current.Request.Form[strname].ToString());
            }
        }

        /// <summary>
        /// 得到Post请求参数
        /// </summary>
        /// <param name="strname">参数名称</param>
        /// <returns>返回整型</returns>
        public static int RequestFormInt(string strname)
        {
            return Convert.ToInt32(HttpContext.Current.Request.Form[strname].ToString());
        }

        /// <summary>
        /// 得到Post请求参数
        /// </summary>
        /// <param name="strname">参数名称</param>
        /// <returns>返回布尔类型</returns>
        public static bool RequestFromBool(string strname)
        {
            return Convert.ToBoolean(RequestFormInt(strname, 0));
        }
    }
}
